//
//  MapViewController.m
//  OneDay
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 oneDay. All rights reserved.
//

#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewController ()<MKMapViewDelegate, CLLocationManagerDelegate>

@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, copy) NSString *lat; // 经度
@property (nonatomic, copy) NSString *lng; // 纬度

@end

@implementation MapViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpdataMapView];
    self.lat = self.locationDic[@"lat"];
    self.lng = self.locationDic[@"lng"];
    // Do any additional setup after loading the view.
}

// 创建地图
- (void)setUpdataMapView
{
    self.locationManager  = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    // 定位精度
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 设置多少米更新一次
    self.locationManager.distanceFilter = 20;
    // 什么时候开始请求信息
    [self.locationManager requestAlwaysAuthorization];
    [self.locationManager startUpdatingLocation];
    
    _mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
    // 经纬度
    _mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2DMake([_lat floatValue], [_lng floatValue]),  MKCoordinateSpanMake(5, 5));
    _mapView.mapType = MKMapTypeStandard;
    
    _mapView.delegate = self;
    _mapView.showsPointsOfInterest = YES;
    _mapView.showsUserLocation = YES;
    [_mapView setCenterCoordinate:CLLocationCoordinate2DMake([_lat floatValue], [_lng floatValue]) animated:YES];
    
    
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake([_lat floatValue], [_lng floatValue]), 1000, 1000);
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    // 设置检索参数
    request.region = region;
    // 兴趣点关键字
    request.naturalLanguageQuery = @"hotal";
    
    CLLocationCoordinate2D locssss = CLLocationCoordinate2DMake(40.0, 116.55);
   MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = locssss;
    [_mapView addAnnotation:point];
    
    
    // 初始化检索 
    MKLocalSearch *ser = [[MKLocalSearch alloc] initWithRequest:request];
    [ser startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
        NSArray *array = [NSArray arrayWithArray:response.mapItems];
        for (int i = 0; i<array.count; i++) {
            MKMapItem *item = array[i];
            [MKMapItem mapItemForCurrentLocation];
            MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
            point.title = item.name;
            point.subtitle = item.phoneNumber;
            point.coordinate = item.placemark.coordinate;
//            [_mapView addAnnotation:point];
        }
    }];
    [self.view addSubview:_mapView];
    
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"anno"];
    view.canShowCallout = YES;
    return view;
}


- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
